package com.example.mrwang.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import util.OrderRefundPopuWindow;

public class MainActivity extends Activity implements PopupWindow.OnDismissListener {
    private OrderRefundPopuWindow window;
    LinearLayout lin;
    RelativeLayout linBg;

    String TAG = "POPWINDOW";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linBg = (RelativeLayout) findViewById(R.id.rel_bg);
        lin = (LinearLayout) findViewById(R.id.lin_foot);
        window = new OrderRefundPopuWindow(MainActivity.this);
        lin.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                // 设置背景颜色变暗
                backgroundAlpha(0.4f);
                // window.showAtLocation(viewBg, Gravity.BOTTOM, 0, lin.getHeight());
                window.showAsDropDown(view);
                //  window.showAsDropDown(viewBg,0,lin.getHeight(),Gravity.BOTTOM);

            }
        });

        window.setOnDismissListener(this);

    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        Log.d(TAG, "bgAlpha: ." + bgAlpha);
//        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        lp.alpha = bgAlpha;
//        getWindow().setAttributes(lp);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        linBg.setAlpha(bgAlpha);
    }

    @Override
    public void onDismiss() {
        Log.d(TAG, "onDismiss: .");
        backgroundAlpha(1.0f);
    }
}
